home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / AmiVoGL_MDEV.lha / examples / patches.c < prev    next >
C/C++ Source or Header  |  1994-07-15  |  6KB  |  302 lines

  1.  
  2. #include <stdio.h>
  3.  
  4. #ifdef SGI
  5. #include "gl.h"
  6. #include "device.h"
  7. #include "hershey.h"
  8. #else
  9. #include "vogl.h"
  10. #include "vodevice.h"
  11. #endif
  12.  
  13. /*
  14.  *    Draws patches of various bases
  15.  */
  16.  
  17. /*
  18.  * patch basis types
  19.  */
  20.  
  21. Matrix    bezier = {
  22.     {-1.0,    3.0,    -3.0,    1.0},
  23.     {3.0,    -6.0,    3.0,    0.0},
  24.     {-3.0,    3.0,    0.0,    0.0},
  25.     {1.0,    0.0,    0.0,    0.0} 
  26. };
  27.  
  28. Matrix    cardinal = { /* Or catmull-rom */
  29.     {-0.5,    1.5,    -1.5,    0.5},
  30.     {1.0,    -2.5,    2.0,    -0.5},
  31.     {-0.5,    0.0,    0.5,    0.0},
  32.     {0.0,    1.0,    0.0,    0.0}
  33. };
  34.  
  35. Matrix    bspline = {
  36.     {-1.0 / 6.0,    3.0 / 6.0,    -3.0 / 6.0,    1.0 / 6.0},
  37.     {3.0 / 6.0,    -6.0 / 6.0,    3.0 / 6.0,    0.0},
  38.     {-3.0 / 6.0,    0.0,        3.0 / 6.0,    0.0},
  39.     {1.0 / 6.0,    4.0 / 6.0,    1.0 / 6.0,    0.0}    
  40. };
  41.  
  42. Matrix    power = {
  43.     {1.0, 0.0, 0.0, 0.0},
  44.     {0.0, 1.0, 0.0, 0.0},
  45.     {0.0, 0.0, 1.0, 0.0},
  46.     {0.0, 0.0, 0.0, 1.0}
  47. };
  48.  
  49. #ifdef ORIGINAL
  50. float *basis[] = {    (float *)bezier,
  51.             (float *)cardinal,
  52.             (float *)bspline,
  53.             (float *)power
  54.         };
  55. #endif
  56.  
  57. Matrix    x1 = {
  58.     {0.0,   0.2588,   0.5,   0.7071},
  59.     {0.0,   0.51764,  1.0,   1.4142},
  60.     {0.0,   0.51764,  1.0,   1.4142},
  61.     {0.0,   0.2588,   0.5,   0.7071}
  62. };
  63.  
  64. Matrix    y1 = {
  65.     {1.0,   0.966,   0.866,  0.7071},
  66.     {2.0,   1.9318,  1.732,  1.4142},
  67.     {2.0,   1.9318,  1.732,  1.4142},
  68.     {1.0,   0.966,   0.866,  0.7071}
  69. };
  70.  
  71. Matrix    z1 = {
  72.     {1.0,   1.0,     1.0,    1.0},
  73.     {1.0,   1.0,     1.0,    1.0},
  74.     {0.0,   0.0,     0.0,    0.0},
  75.     {0.0,   0.0,     0.0,    0.0}
  76. };
  77.  
  78. Matrix    x2 = {
  79.     {0.7071, 0.8660, 0.9660, 1.0},
  80.     {1.4142, 1.7320, 1.932,  2.0},
  81.     {1.4142, 1.7320, 1.932,  2.0},
  82.     {0.7071, 0.8660, 0.9660, 1.0}
  83. };
  84.  
  85. Matrix    y2 = {
  86.     {0.7071, 0.5,    0.2588, 0.0},
  87.     {1.4142, 1.0,    0.5176, 0.0},
  88.     {1.4142, 1.0,    0.5176, 0.0},
  89.     {0.7071, 0.5,    0.2588, 0.0}
  90. };
  91.  
  92. Matrix    z2 = {
  93.     {1.0,   1.0,     1.0,    1.0},
  94.     {1.0,   1.0,     1.0,    1.0},
  95.     {0.0,   0.0,     0.0,    0.0},
  96.     {0.0,   0.0,     0.0,    0.0}
  97. };
  98.  
  99. char *labels[] = {
  100.         "Bezier Patch(es)",
  101.         "Cardinal Patch(es)",
  102.         "B-Spline Patch(es)",
  103.         "'Power' Patch(es)"
  104.         };
  105.  
  106. /* ---------------------------------------------------------------------
  107.  * Prototypes:
  108.  */
  109. int main(void);                                        /* patches.c       */
  110. void drawhull( Matrix, Matrix, Matrix);                /* patches.c       */
  111. void axes(void);                                       /* patches.c       */
  112.  
  113. /* ---------------------------------------------------------------------
  114.  * Source Code:
  115.  */
  116.  
  117. /*
  118.  * demonstrate patches
  119.  */
  120.  
  121. int main(void)
  122. {
  123.     int    i;
  124.     short    val;
  125.     Screencoord    minx, maxx, miny, maxy;
  126.  
  127.     hfont("times.r");
  128.  
  129.     winopen("patches");
  130.  
  131.     unqdevice(INPUTCHANGE);
  132.     qdevice(KEYBD);
  133.  
  134.     color(BLACK);
  135.     clear();
  136.  
  137.     /*
  138.      * Set up two viewports (They actually overlap)
  139.      */
  140.     getviewport(&minx, &maxx, &miny, &maxy);
  141.  
  142.     viewport(minx, (maxx - minx) / 10 * 6, miny, (maxy - miny) / 10 * 6);
  143.     ortho(-2.0, 5.0, -2.0, 5.0, -2.0, 5.0);
  144.     lookat(0.0, 0.0, 0.0, -3.0, 2.0, -4.0, 0);
  145.     /*
  146.      * Save it 
  147.      */
  148.     pushviewport();
  149.     pushmatrix();
  150.  
  151.     viewport((maxx - minx) / 10 * 2, maxx, (maxy - miny) / 10 * 2, maxy);
  152.     ortho(-2.0, 5.0, -2.0, 5.0, -2.0, 5.0);
  153.     lookat(0.0, 0.0, 0.0, 3.0, 2.0, -4.0, 0);
  154.  
  155.     htextsize(0.4, 0.4);
  156.  
  157.     /*
  158.      * patchcurves provides a number of curves in the t and u
  159.      * directions. patchprecision gives the minimum number of line
  160.      * segments making up the curves in the t and u directions. The
  161.      * actual number of linesegments in t or u is equal to the closest
  162.      * integer multiple of the number of curves, > nsegs, in t or u,
  163.      * greater than or equal to the number set by patchprecision in u or
  164.      * t. eg. curves in t will be made up of 21 line segments so that we
  165.      * can match up the 7 curves in u; curves in u will have 24 as 4 by 5
  166.      * gives 20.
  167.      */
  168.     patchcurves(4L, 7L);
  169.     patchprecision(20L, 20L);
  170.  
  171.     for (i = 0; i < 4; i++) {
  172.  
  173.         axes();
  174.  
  175.  
  176.         /*
  177.          * patchbasis sets the basis matrices for the t and u
  178.          * functions
  179.          * 
  180.          */
  181. #ifdef ORIGINAL
  182.         defbasis((short)i, basis[i]);
  183. #else
  184.         switch(i) {
  185.         case 0: defbasis((short)i, bezier); break;
  186.         case 1: defbasis((short)i, cardinal); break;
  187.         case 2: defbasis((short)i, bspline); break;
  188.         case 3: defbasis((short)i, power); break;
  189.             }
  190. #endif
  191.         patchbasis((long)i, (long)i);
  192.  
  193.         /* 
  194.          * Draw with viewport 2
  195.          */
  196.         move(0.0, 4.0, 0.0);
  197.         hcharstr(labels[i]);
  198.  
  199.         /*
  200.          * now draw the patches according to the geometry matrices in
  201.          * x1, y1, and z1, x2, y2, z2.
  202.          */
  203.         drawhull(x1, y1, z1);
  204.         patch(x1, y1, z1);
  205.  
  206.         drawhull(x2, y2, z2);
  207.         patch(x2, y2, z2);
  208.  
  209.         /*
  210.          * Now with viewport 1
  211.          */
  212.         popviewport();
  213.         popmatrix();
  214.  
  215.         axes();
  216.  
  217.         move(0.0, 4.0, 0.0);
  218.         hcharstr(labels[i]);
  219.  
  220.         /*
  221.          * now draw the patches according to the geometry matrices in
  222.          * x1, y1, and z1, x2, y2, z2.
  223.          */
  224.         drawhull(x1, y1, z1);
  225.         patch(x1, y1, z1);
  226.  
  227.         drawhull(x2, y2, z2);
  228.         patch(x2, y2, z2);
  229.  
  230.         qread(&val);
  231.  
  232.         /*
  233.          * Save viewport 1, after clearing it, again and reset
  234.          * to viewport 2
  235.          */
  236.         color(BLACK);
  237.         clear();
  238.  
  239.         pushviewport();
  240.         pushmatrix();
  241.  
  242.         viewport((maxx - minx) / 10 * 2, maxx, (maxy - miny) / 10 * 2, maxy);
  243.         ortho(-1.5, 5.0, -1.5, 5.0, -1.5, 5.0);
  244.         lookat(0.0, 0.0, 0.0, 3.0, 2.0, -4.0, 0);
  245.  
  246.         clear();
  247.     }
  248.  
  249.     gexit();
  250. }
  251.  
  252. /*
  253.  * drawhull
  254.  *
  255.  *    draw the hull for x, y, and z.
  256.  */
  257. void drawhull(
  258.   Matrix x,
  259.   Matrix y,
  260.   Matrix z)
  261. {
  262.     int    i, j;
  263.     char    buf[2];
  264.  
  265.     color(MAGENTA);    
  266.  
  267.     for (i = 0; i < 4; i++) {
  268.         move(x[i][0], y[i][0], z[i][0]);
  269.         for (j = 1; j < 4; j++)
  270.             draw(x[i][j], y[i][j], z[i][j]);
  271.     }
  272.  
  273.     for (i = 0; i < 4; i++) {
  274.         move(x[0][i], y[0][i], z[0][i]);
  275.         for (j = 1; j < 4; j++) 
  276.             draw(x[j][i], y[j][i], z[j][i]);
  277.     }
  278.  
  279.     /* 
  280.       * Set color for The patch
  281.      */
  282.     color(GREEN);
  283. }
  284.  
  285. /*
  286.  * axes
  287.  *
  288.  *    draw the axes
  289.  */
  290. void axes(void)
  291. {
  292.     color(YELLOW);
  293.     move(0.0, 0.0, 0.0);
  294.     draw(4.0, 0.0, 0.0);
  295.  
  296.     move(0.0, 0.0, 0.0);
  297.     draw(0.0, 4.0, 0.0);
  298.  
  299.     move(0.0, 0.0, 0.0);
  300.     draw(0.0, 0.0, 4.0);
  301. }
  302.